package com.realestate.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.mysema.query.BooleanBuilder;
import com.mysema.query.jpa.JPQLQuery;
import com.mysema.query.jpa.impl.JPAQuery;
import com.realestate.constants.ComTxManagerName;
import com.realestate.constants.ComUnitName;
import com.realestate.constants.RecordStatus;
import com.realestate.model.BrtbBuilding;
import com.realestate.model.QBrtbBuilding;
import com.trg.dao.jpa.GenericDAOImpl;

@Repository
@Transactional(value = ComTxManagerName.COM_TRANSACTION_MANAGER)
public class BuildingDaoImpl extends GenericDAOImpl<BrtbBuilding, Long> implements BuildingDao{
	
	@SuppressWarnings("unused")
	private static final Logger LOGGER = Logger.getLogger(BuildingDaoImpl.class);
	
	@Override
	@PersistenceContext(unitName = ComUnitName.COM_UNIT)
	public void setEntityManager(EntityManager entityManager) {
		super.setEntityManager(entityManager);
	}
	
	@Override
	public List<BrtbBuilding> getAllBuilding() {
		JPQLQuery query = new JPAQuery(em());
		QBrtbBuilding buildings = QBrtbBuilding.brtbBuilding;
		
		query.from(buildings);
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(buildings.recordStatus.eq(RecordStatus.O));
		
		query.where(where);
		
		List<BrtbBuilding> interiorsResult = query.list(buildings);
		return interiorsResult;
	}

	@Override
	public BrtbBuilding getBuildingByCode(String code) {
		JPQLQuery query = new JPAQuery(em());
		QBrtbBuilding buildings = QBrtbBuilding.brtbBuilding;
		
		query.from(buildings);
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(buildings.recordStatus.eq(RecordStatus.O))
			.and(buildings.buildingCode.eq(code));
		
		query.where(where);
		
		BrtbBuilding interiorsResult = query.uniqueResult(buildings);
		return interiorsResult;
	}
}
